class RateController {

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        [rateInstanceList: Rate.list(params), rateInstanceTotal: Rate.count()]
    }

    def create = {
        def rateInstance = new Rate()
        rateInstance.properties = params
        return [rateInstance: rateInstance]
    }

    def save = {
        def rateInstance = new Rate(params)
        if (!rateInstance.hasErrors() && rateInstance.save()) {
            flash.message = "rate.created"
            flash.args = [rateInstance.id]
            flash.defaultMessage = "Rate ${rateInstance.id} created"
            redirect(action: "show", id: rateInstance.id)
        }
        else {
            render(view: "create", model: [rateInstance: rateInstance])
        }
    }

    def show = {
        def rateInstance = Rate.get(params.id)
        if (!rateInstance) {
            flash.message = "rate.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Rate not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [rateInstance: rateInstance]
        }
    }

    def edit = {
        def rateInstance = Rate.get(params.id)
        if (!rateInstance) {
            flash.message = "rate.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Rate not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [rateInstance: rateInstance]
        }
    }
    
    def enable = {
    	params.disabled = false
    	rateService.update(params)
    }
    
    def disable = {
    	params.disabled = true
    	rateService.update(params)
    }

    def update = {
        def rateInstance = Rate.get(params.id)
        if (rateInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (rateInstance.version > version) {
                    
                    rateInstance.errors.rejectValue("version", "rate.optimistic.locking.failure", "Another user has updated this Rate while you were editing")
                    render(view: "edit", model: [rateInstance: rateInstance])
                    return
                }
            }
            rateInstance.properties = params
            if (!rateInstance.hasErrors() && rateInstance.save()) {
                flash.message = "rate.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Rate ${params.id} updated"
                redirect(action: "show", id: rateInstance.id)
            }
            else {
                render(view: "edit", model: [rateInstance: rateInstance])
            }
        }
        else {
            flash.message = "rate.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Rate not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def rateInstance = Rate.get(params.id)
        if (rateInstance) {
            try {
                rateInstance.delete()
                flash.message = "rate.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Rate ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "rate.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Rate ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "rate.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Rate not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
